<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/9
 * Time: 09:37
 * used to test file and upload
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Common as helper;
use Illuminate\Support\Facades\Input;
use Laravel\Lumen\Routing\Controller as BaseController;

class FileController extends BaseController{

    // normal
    public function uploads(Request $request){
//        $file = Input::file('file');
//        echo $file->getClientOriginalExtension();
//        dd($file);exit;
        $file = $request->file('file');
        if($request->hasFile('file')){
            if($file->isValid()){
                // 获取文件相关信息
                $originalName = $file->getClientOriginalName(); // 文件原名
                $ext = $file->getClientOriginalExtension();     // 扩展名
                $realPath = $file->getRealPath();   //临时文件的绝对路径
                $type = $file->getClientMimeType();     // image/jpeg;
                $size = $file->getClientSize();
                // 上传文件
                $filename = date('Ymdhis') . '-' . uniqid() . '.' . $ext;
                //$res = $request->file('file')->move('../storage/app/uploads/',$filename);
                $res = $file->storeAs('images',$filename);
                var_dump($res);
            }
        }
    }

    public function valid_uploads(Request $request){
        $result = array('code'=>"-100",'msg'=>'error');
        if($request->hasFile('file')){
            $file = $request->file('file');
            if($file->isValid()){
                $size = $file->getClientSize();
                $ext = $file->getClientOriginalExtension();
                $exts = array("jpeg", "png", "bmp", "gif","jpg","JPG");
                if(in_array($ext,$exts)){
                    if($size<= 2097152){
                        $filename = date('Ymdhis') . '-' . uniqid() . '.' . $ext;
                        $res = $request->file('file')->move('../storage/app/uploads/',$filename);
                        $result = array('code'=>'100','msg'=>"success");
                    }else{
                        $result = array('code'=>'-98','msg'=>"image's size is too large");
                    }
                }else{
                    $result = array('code'=>'-99','msg'=>"image's type not be allowed");
                }
            }
        }
        echo json_encode($result);
    }

    public function files_uploads(Request $request){

        if($request->hasFile('file')){
            $files = $request->file('file');
            $imgs = array();
            foreach($files as $key=>$file){
                if($file->isValid()){
                    $size = $file->getClientSize();
                    $ext = $file->getClientOriginalExtension();
                    $exts = array("jpeg", "png", "bmp", "gif","jpg","JPG");
                    if(in_array($ext,$exts)){
                        if($size<= 2097152){
                            $filename = date('Ymdhis') . '-' . uniqid() . '.' . $ext;
                            $file->move('../storage/app/uploads/',$filename);
                            $imgs[] = $filename;
                        }else{
                            $imgs[] = $key." too large";
                        }
                    }else{
                    }
                }
            }
        }
        $result = array('code'=>"100",'msg'=>$imgs);
        echo json_encode($result);
    }

}